package cn.biansc.service.impl;

import cn.biansc.service.IWorkflowService;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;
@Service
public class WorkflowServiceImpl implements IWorkflowService {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private FormService formService;
    @Autowired
    private HistoryService historyService;

    /**部署流程定义*/
    @Override
    public void saveNewDeploye(InputStream inputStream, String filename) {
        try {
            /* 2：将File类型的文件转化成ZipInputStream流 */
            ZipInputStream zipInputStream = new ZipInputStream(inputStream);
            repositoryService.createDeployment()//创建部署对象
                    .name(filename)//添加部署名称
                    .addZipInputStream(zipInputStream)//
                    .deploy();//完成部署
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**查询部署对象信息，对应表（act_re_deployment）*/
    @Override
    public List<Deployment> findDeploymentList() {
        List<Deployment> list = repositoryService.createDeploymentQuery()//创建部署对象查询
                .orderByDeploymenTime().asc()//
                .list();
        return list;
    }

    @Override
    public List<ProcessDefinition> findProcessDefinition() {
        List<ProcessDefinition> list= repositoryService.createProcessDefinitionQuery()
                .orderByProcessDefinitionVersion()
                .asc()
                .list();

        return list;

    }
    @Override
    public Object getStartFormData(String id){

        Object startFormData = formService.getRenderedStartForm(id);
        return  startFormData;
    }
    /**使用部署对象ID，删除流程定义*/
    @Override
    public void deleteProcessDefinitionByDeploymentId(String deploymentId) {
        repositoryService.deleteDeployment(deploymentId, true);
    }
    //使用当前用户名查询正在执行的任务表，获取当前任务的集合List<Task>
    @Override
    public List<Task> findTaskListByName(String name) {
        List<Task> list = taskService.createTaskQuery()//
                .taskAssignee(name)//指定个人任务查询
                .orderByTaskCreateTime().asc()//
                .list();
        return list;
    }

}
